/**
 * plugins/index.ts
 *
 * Automatically included in `./src/main.ts`
 */

import type { Component } from 'vue'
import type { Router } from 'vue-router' // Specify weigh
import * as Sentry from '@sentry/vue'
import { createPinia } from 'pinia'
import { PiniaColada } from '@pinia/colada'
// Composables
import { createApp } from 'vue'
import i18n from './i18n'
// Plugins
import vuetify from './vuetify'
import { initNotificationContainer } from './notifications'

// Styles

import '@fontsource/roboto/200.css' // Specify weight
import '@fontsource/roboto/300.css' // Specify weight
import '@fontsource/roboto/400.css' // Specify weight
import '@fontsource/roboto/500.css' // Specify weight
import '@fontsource/roboto/700.css' // Specify weight
import '@fontsource/roboto/900.css'

export function registerPlugins(component: Component, router?: Router) {
  const app = createApp(component)

  app.use(vuetify).use(createPinia()).use(i18n).use(PiniaColada)

  // 注意：数据库现在由主进程管理，渲染进程不需要初始化

  if (router) app.use(router)

  // 只在非开发环境中启用 Sentry
  if (process.env.NODE_ENV === 'development') {
    console.log('开发环境中已禁用 Sentry (渲染进程)')
  } else {
    Sentry.init({
      app,
      dsn: process.env.SENTRY_DSN,
      environment: process.env.NODE_ENV || 'development', // 设置环境
      release: `field-flint@${process.env.npm_package_version}`, // 设置发布版本
      // Setting this option to true will send default PII data to Sentry.
      // For example, automatic IP address collection on events
      sendDefaultPii: true,
      integrations: [
        Sentry.replayIntegration(),
        Sentry.browserTracingIntegration() // 添加性能监控
      ],
      // 性能监控配置
      tracesSampleRate: process.env.NODE_ENV === 'production' ? 0.1 : 1,
      // Session Replay 配置
      replaysSessionSampleRate: process.env.NODE_ENV === 'production' ? 0.1 : 1,
      replaysOnErrorSampleRate: 1, // 错误时 100% 录制
      // 调试配置
      debug: false, // 在生产环境中关闭调试
      // 错误过滤
      beforeSend(event) {
        return event
      }
    })
  }
  // Mount global notification container (separate Vue app)
  initNotificationContainer()

  app.mount('#app')

  return app
}
